package com.shen.server.utils;

import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * fastDFS工具类
* @date: 2022/4/25
* @return
*/

public class FastDFSUtils {

    private static Logger logger = LoggerFactory.getLogger(FastDFSUtils.class);


    /*
    * 初始化客户端
    * ClientGlobal_init 读取配置文件，并初始化对应的属性
    * */
    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            logger.error("初始化FastDFS失败！",e);
        }
    }




    /*
    * 生成TrackServer服务器
    * */
    private static TrackerServer getTrackerServer() throws IOException {

        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        return trackerServer;

    }
    /*
     * 生成storage客户端
     * */
    private static Object getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }

    /*
    * 上传文件
    * */
    public static String[] upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        logger.info("文件名：",originalFilename);
        StorageClient storageClient = null;
        String[] uploadReults = null;
        try {
            //获取storage客户端
            storageClient = (StorageClient) getStorageClient();
            uploadReults = storageClient.upload_file(file.getBytes(),
                    originalFilename.substring(originalFilename.lastIndexOf(".") + 1)
                    , null);

        } catch (Exception e) {
            logger.error("文件上传失败",e.getMessage());
        }
        if (null == uploadReults && null != storageClient){
            logger.error("上传失败！",storageClient.getErrorCode());
        }
        return uploadReults;
    }

    /*
    * 获取文件信息
    * */
    public static FileInfo getFileInfo(String groupName,String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = (StorageClient)getStorageClient();
            return storageClient.get_file_info(groupName,remoteFileName);
        } catch (Exception e) {
            logger.error("文件获取失败", e.getMessage());
        }
        return null;
    }

    public static InputStream downFile(String groupName,String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = (StorageClient)getStorageClient();
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            InputStream inputStream = new ByteArrayInputStream(fileByte);
            return inputStream;
        } catch (Exception e) {
            logger.error("文件下载失败", e.getMessage());
        }
        return null;
    }

    public static void  deleteFile(String groupName,String remoteFileNName){
        StorageClient storageClient = null;
        try {
            storageClient = (StorageClient)getStorageClient();
            storageClient.delete_file(groupName,remoteFileNName);
        } catch (Exception e) {
            logger.error("文件删除失败", e.getMessage());
        }

    }

    /*
    * 获取文件路径
    * */
    public static String getTrackerUrl(){
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = null;
        StorageServer storeStorage = null;
        try {
            trackerServer = trackerClient.getTrackerServer();
            storeStorage = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            logger.error("文件路径获取失败",e.getMessage());
        }
        return "http://"+storeStorage.getInetSocketAddress().getHostName()+":8888/";
    }
}
